﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace DGIS.Map.Converts
{
    public class FileSizeConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double fileSize = (double)value;
            string fileSizeStr = string.Empty;

            double size = Math.Round(System.Convert.ToDouble(fileSize) / 1024, 5);
            if (size == 0)
            {
                fileSizeStr = "";
            }
            else if (fileSize <1024)
            {
                fileSizeStr = fileSize.ToString() + "B";
            }
            else if (size <1024 && size >=1 )
            {
                size = Math.Round(System.Convert.ToDouble(fileSize) / 1024, 2);
                fileSizeStr = size.ToString() + "KB";
            }
            else if (size<1024*1024 && size >= 1024)
            {
                size = Math.Round(System.Convert.ToDouble(fileSize) / (1024 * 1024), 2);
                fileSizeStr = size.ToString() + "MB";
            }
            else
            {
                size = Math.Round(System.Convert.ToDouble(fileSize) / (1024 * 1024 * 1024), 2);
                fileSizeStr = size.ToString() + "G";
            }

            return fileSizeStr;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
